﻿using System.Drawing;
using System.Drawing.Imaging;
using System.Web.Mvc;
using WebsiteImages.Helpers;
using WebsiteImages.Models;
using WebsiteImages.Repository;

namespace WebsiteImages.Controllers
{
    public class WebsiteImageController : Controller
    {
        private readonly IWebsiteImageRepository _repository;

        public WebsiteImageController() : this (new WebsiteImageRepository())
        {

        }

        public WebsiteImageController(IWebsiteImageRepository websiteImageRepository)
        {
            _repository = websiteImageRepository;
        }

        // GET: /WebsiteImage/Create

        public ActionResult Create()
        {
            return View();
        }

        // POST: /WebsiteImage/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(UploadImage model)
        {
            if (ModelState.IsValid)
            {
                var original = Image.FromStream(model.File.InputStream) as Bitmap;

                // Creates the image with the cropping
                var tmpImage = ImageHelper.CreateImage(original, model.X, model.Y, model.Width, model.Height);

                // create a large image but of a known size and dont' rely on css (gives appropriate size image).
                var img = ImageHelper.ScaleImage(tmpImage, 600, 800);
                var image = ImageHelper.ImageToBytes(img, ImageFormat.Jpeg);

                // create a thumbnail image
                img = ImageHelper.ScaleImage(img, 100, 133);
                var thbImage = ImageHelper.ImageToBytes(img, ImageFormat.Jpeg);

                var newWebsiteImage = new WebsiteImage
                {
                    Image = image,
                    ThbImage = thbImage
                };

                _repository.Add(newWebsiteImage);
                return RedirectToAction("Index");
            }
            return View();
        }

        public ActionResult Index()
        {
            var model = _repository.WebsiteImages();
            return View(model);
        }

        public ActionResult Details(int id)
        {
            return View(id);
        }

        public FileContentResult GetImage(int id)
        {
            var img = _repository.Find(id);
            if (img == null) return null;
            return new FileContentResult(img.Image, "image/jpeg");
        }

        public FileContentResult GetImageThb(int id)
        {
            var img = _repository.Find(id);
            if (img == null) return null;
            return new FileContentResult(img.ThbImage, "image/jpeg");
        }

        //
        // GET: /WebsiteImage/Delete/5

        public ActionResult Delete(int id = 0)
        {
            WebsiteImage websiteimage = _repository.Find(id);
            if (websiteimage == null) return HttpNotFound();
            return View(websiteimage);
        }

        //
        // POST: /WebsiteImage/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            _repository.Delete(id);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _repository.Dispose();
            base.Dispose(disposing);
        }
    }
}